<template>
  <div class="chart2">
    <div class="chart2-header">
      <div>年：<el-date-picker size="small" v-model="searchYear" value-format="yyyy" type="year" placeholder="选择年份" />
      </div>
      <div>月：<el-date-picker size="small" v-model="searchMonth" value-format="yyyy-MM" type="month"
          placeholder="选择月份" /></div>
      <div v-if="roles.includes('admin')">区域：<el-select size="small" clearable v-model="origin" placeholder="选择区域">
          <el-option label="中国大陆" value="1" />
          <el-option label="台湾地区" value="2" />
          <el-option label="日本" value="3" />
        </el-select></div>

    </div>
    <div id="chart2"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { bussiness } from '@/api/system/order'

export default {
  data() {
    return {
      searchYear: '',
      searchMonth: '',
      month: '',
      origin: '',
      areaList: [],
      roles: []


    }
  },
  created() {
    this.roles = this.$store.getters.roles;

  },
  watch: {
    searchYear: {
      handler(newVal) {
        this.init()
      },
    },
    searchMonth: {
      handler(newVal) {
        if (newVal) {
          this.searchYear = newVal.split('-')[0]
          this.init()
        } else {
          this.searchYear = ''
          this.init()
        }
      },
    },
    origin: {
      handler(newVal) {
        this.origin = newVal
        this.init()

      },
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      const chartDom = document.getElementById('chart2')
      const myChart = echarts.init(chartDom)
      myChart.showLoading()
      bussiness({ searchYear: this.searchYear, searchMonth: this.searchMonth, origin: this.origin }).then(res => {
        this.initChart(res.data)
        myChart.hideLoading()
      })
    },
    initChart(res) {
      const chartDom = document.getElementById('chart2')
      const myChart = echarts.init(chartDom)
      const xData = res.map(item => item.deptName)
      // localStorage.setItem("ditou",JSON.stringify(res))
      this.areaList = res.map(item => {
        return {
          id: item.deptName,
          name: item.deptName
        }
      })
      const yData = res.map(item => item.newJpyAmount)



      const option = {
        title: {
          text: '营业额',
          left: 'center'
        },
        grid: {
          left: '3%',
          right: '4%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          bottom: '0'
        },
        xAxis: {
          type: 'category',
          data: xData
        },
        yAxis: {
          type: 'value',
          min: 0,
        },
        series: [

          {
            type: 'bar',
            data: yData,
            itemStyle: {
              color: '#43A2F6'
            }
          }
        ]
      }

      option && myChart.setOption(option)

      // 添加响应式调整
      window.addEventListener('resize', () => {
        myChart.resize()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.chart2 {
  width: 100%;

  #chart2 {
    height: 100%;
  }

  .chart2-header {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 12px;
  }
}
</style>
